<?php if ( ! defined('APPPATH')) exit('No direct script access allowed');
/**
 *      [PinTuXiu] (C)2001-2099 ONightjar.com Pintuxiu.com.
 *      This is NOT a freeware, use is subject to license terms
*/
class group_controller extends PZ_Controller{

	public function __construct() {
		parent::__construct();
		$this->authfunctions = array('join','leave','delete');
		// 		$this->seo_title(T('hobby_group'));
		// 		$this->seo_keyword(T('hobby_group'));
		// 		$this->seo_description(T('hobby_group'));
	}

	public function listing(){
		$num_per_page = 10;
		$ptx_group = load_db("group");
		$category_id =  $this->param("cid");
		$sort =  $this->param("sort");
		$args = array();
		if($category_id){
			$this->category_id = $conditions['category_id'] = $args['cid'] = $category_id;
		}

		if($sort){
			$this->sort = $args['sort'] = $sort;
		}
		if($sort=='dtime'){
			$sort = " ptx_group.create_time DESC ";
		}elseif($sort=='atime'){
			$sort = " ptx_group.create_time ASC ";
		}elseif($sort=='dmember'){
			$sort = " ptx_group.total_member DESC ";
		}
		$this->page = $this->param('page','1');
		$groups = $ptx_group->search($conditions,$this->page,$num_per_page,null,$sort);
		$pager = &load_class('Pager', 'core');
		$this->pages = $pager->createPages('group/listing',$args);
		$ptx_share = load_db('share');
		foreach ($groups as $key=>$group){
			$share_conditions['keyword'] = 'group'.$group['group_id'].'g';
			$shares = $ptx_share->count($share_conditions);
			$groups[$key]['total_share'] = $shares;
		}
		$this->groups = $groups;
		$this->newest10 = $ptx_group->search_no_page(null,null,null,5);
		$this->load_seo('group');
		$this->output("group/listing");
	}

	public function join(){
		if($groupid = $this->param("groupid")){
			$this->group = load_db("group")->find_one(array('group_id'=>$groupid));
		}

		if(!$this->group){
			$this->ajax_failed_response(lang('404_error_content','tip'));
			return;
		}

		$data['user_id'] = $this->cuser['uid'];
		$data['group_id'] = $groupid;
		$ugroupdb = load_db("group/ugroup");
		if(!$ugroupdb->find($data)){
			$ugroupdb->add_one($data);
		}
		$this->ajax_success_response(null, lang('200_operatesuccess','tip'));
	}

	public function leave(){
		if($groupid = $this->param("groupid")){
			$this->group = load_db("group/ugroup")->find_one(array('user_id'=>$this->cuser['uid'],'group_id'=>$groupid)," pgroup.*,ptx_user_group.is_admin ");
		}

		if(!$this->group){
			$this->ajax_failed_response(lang('404_error_content','tip'));
			return;
		}
		$data['user_id'] = $this->cuser['uid'];
		$data['group_id'] = $groupid;
		if(load_db("group/ugroup")->remove_one($data)){
			$this->ajax_success_response(null, lang('200_operatesuccess','tip'));
		}else{
			$this->ajax_failed_response(lang('illegal_operation','tip'));
		}
	}

	public function delete(){
		if($groupid = $this->param("groupid")){
			$this->group = load_db("group")->find_one(array('group_id'=>$groupid));
		}

		if(!$this->group){
			$this->redirect(site_url('group/listing'), lang('404_error_content','tip'));
			return;
		}

		if($this->isadmin||$this->iseditor||$this->group['user_id']==$this->cuser['uid']){
			load_db('group')->delete(array('group_id'=>$groupid));
			load_db("group/ugroup")->delete(array('group_id'=>$groupid));
			$this->redirect(site_url('home/groups'), lang('200_deletesuccess','tip'));
		}else{
			$this->message(lang('error','tip'), lang('illegal_operation','tip'));
		}
	}

	public function show(){
		$this->lastest();
	}

	public function hot(){
		$this->output_pin('hot', " ptx_share.total_like DESC ");
	}

	public function lastest(){
		$this->output_pin('lastest', " ptx_share.create_time DESC ");
	}

	public function goods(){
		$conditions['type'] = 'goods';
		$this->output_pin('goods', " ptx_share.create_time DESC ",$conditions);
	}

	public function video(){
		$conditions['type'] = 'video';
		$this->output_pin('video', " ptx_share.create_time DESC ",$conditions);
	}

	public function share(){
		$conditions['type'] = 'share';
		$this->output_pin('share', " ptx_share.create_time DESC ",$conditions);
	}

	public function article(){
		$conditions['type'] = 'article';
		$this->output_pin('article', " ptx_share.create_time DESC ",$conditions);
	}

	private function output_pin($action,$order,$conditions=array()){
		if($groupid =  $this->param("groupid")){
			$this->group = load_db("group")->find_one(array('group_id'=>$groupid));
		}
		if(!$this->group){
			$this->redirect(site_url('group/listing'), lang('404_error_content','tip'));
			return;
		}

		$api = load_api('setting');
		$setting = $api->getSetting('layoutpin');
		$pagesize = $setting['uipin_numperpage']?$setting['uipin_numperpage']:15;
		$numbershowpage = $setting['uipin_numbershowpage']?$setting['uipin_numbershowpage']:0;
		$page = $this->param('page','1');
		$wpage = $this->param('wpage');
		$args = array("page"=>"2","wf"=>"1","groupid"=>$groupid);
		if($wpage&&$numbershowpage){
			$data['page'] = ($wpage-1)*$numbershowpage+$page;
			$args['wpage'] = $data['wpage'] = $wpage;
		}else{
			$data['page'] = $page;
		}
		$data['showpage'] = (($numbershowpage)&&($data['page']%$numbershowpage==0))?'1':'0';

		if($data['page']==1){
			$ugroupdb = load_db("group/ugroup");
			$data['joined'] = $ugroupdb->find_one(array('user_id'=>$this->cuser['uid'],'group_id'=>$groupid));
			$data['ausers'] = $ugroupdb->search_no_page(array('is_admin'=>'1','group_id'=>$groupid),' user.user_id, user.nickname ',null,10);

			$this->seo_title($this->group['group_title']);
			$this->seo_keyword($this->group['keyword']);
			$this->seo_description($this->group['group_desc']);
		}

		//$this->seo_title($this->group['group_title'].T($action));

		$conditions['keyword'] = 'group'.$groupid.'g';

		$data['nextpage_url'] = site_url_param('group/'.$action,$args);
		$shareapi = load_api('share');
		$shares = $shareapi->search($conditions,$data['page'],$pagesize,$order);
		$shares = $shareapi->prepareShares($shares);
		if($setting['uipin_ads']){
			$shares = $shareapi->add_ads($shares);
		}
		$pager = &load_class('Pager', 'core');
		unset($args['wf']);
		$data['pages'] = $pager->createPages('group/'.$action,$args);
		$pageData = $pager->getPager();
		$data['numbershares'] = $pageData['total_count'];
		unset($args['page']);
		$data['wpages'] = $pager->createCustomPages('group/'.$action,$args,'wpage',$pagesize*$numbershowpage);
		$data['tpl_waterfall'] = $this->render('waterfall/pin',array('shares'=>$shares,'showpage'=>$data['showpage']));
		$data['pinsetting'] = $setting;
		$this->output("group/show",$data,$data['page']==1?true:false);
	}

	public function groupmembers(){
		if($groupid =  $this->param("groupid")){
			$this->group = load_db("group")->find_one(array('group_id'=>$groupid));
		}
		if(!$this->group){
			$this->ajax_failed_response(lang('404_error_content','tip'));
			return;
		}
		$page =  $this->param("page",1);
		$members = load_db("group/ugroup")->search(array('group_id'=>$groupid),$page,30," user.user_id, user.nickname ");
		$members = $this->render('group/member', array('members'=>$members));
		$this->ajax_success_response($members);
	}


}